package com.cyys.common.utils.file;

import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * 图片工具
 * 
 * @author lc
 *
 */
public class ImageUtil {

	private static Logger logger = LoggerFactory.getLogger(ImageUtil.class);

	/**
	 * 指定大小进行缩放
	 * 
	 * @param srcUrl
	 *            源图片地址
	 * @param targetUrl
	 *            目标图片地址
	 * @param width
	 *            宽
	 * @param height
	 *            高
	 * @throws IOException
	 */
	public static void resize(String srcUrl, String targetUrl, int width, int height) throws IOException {
		try {
			Thumbnails.of(srcUrl).size(width, height).toFile(targetUrl);
		} catch (IOException e) {
			logger.error("-----读取图片发生异常:{}-----", e.getMessage());
			logger.info("-----尝试cmyk转化-----");
			File cmykJPEGFile = new File(srcUrl);
			try {
				BufferedImage image = ImageIO.read(cmykJPEGFile);
				ImageOutputStream output = ImageIO.createImageOutputStream(cmykJPEGFile);
				if (!ImageIO.write(image, "jpg", output)) {
					logger.info("-----cmyk转化异常:{}-----");
				}
				Thumbnails.of(image).size(width, height).toFile(targetUrl);
				logger.info("-----cmyk转化成功-----");
			} catch (IOException e1) {
				logger.info("-----cmyk转化异常:{}-----", e1.getMessage());
			}
		}

		/*
		 * size(width,height) 若图片横比200小，高比300小，不变
		 * 若图片横比200小，高比300大，高缩小到300，图片比例不变 若图片横比200大，高比300小，横缩小到200，图片比例不变
		 * 若图片横比200大，高比300大，图片按比例缩小，横为200或高为300
		 */

	}

	/**
	 * 按照比例进行缩放
	 * 
	 * @param srcUrl
	 *            源图片地址
	 * @param targetUrl
	 *            目标图片地址
	 * @param num
	 *            质量比例如 0.8
	 * @throws IOException
	 */
	public static void scale(String srcUrl, String targetUrl, double num) throws IOException {
		try {
			Thumbnails.of(srcUrl).scale(num).toFile(targetUrl);
		} catch (IOException e) {
			logger.error("-----读取图片发生异常:{}-----", e.getMessage());
			logger.info("-----尝试cmyk转化-----");
			File cmykJPEGFile = new File(srcUrl);
			try {
				BufferedImage image = ImageIO.read(cmykJPEGFile);
				ImageOutputStream output = ImageIO.createImageOutputStream(cmykJPEGFile);
				if (!ImageIO.write(image, "jpg", output)) {
					logger.info("-----cmyk转化异常:{}-----");
				}
				Thumbnails.of(image).scale(num).toFile(targetUrl);
				logger.info("-----cmyk转化成功-----");
			} catch (IOException e1) {
				logger.info("-----cmyk转化异常:{}-----", e1.getMessage());
			}
		}
	}

	/**
	 * 水印
	 * 
	 * @param srcUrl
	 *            源图片地址
	 * @param targetUrl
	 *            目标图片地址
	 * @param width
	 *            宽
	 * @param height
	 *            高
	 * @param num
	 *            质量比例如 0.8
	 * @param pos
	 *            显示位置: Positions.BOTTOM_RIGHT
	 * @throws IOException
	 */
	public static void watermark(String srcUrl, String targetUrl, int width, int height, float num, Positions pos)
			throws IOException {
		try {
			Thumbnails.of(srcUrl).size(width, height).watermark(pos, ImageIO.read(new File(targetUrl)), num)
					.outputQuality(num).toFile(targetUrl);
		} catch (IOException e) {
			logger.error("-----读取图片发生异常:{}-----", e.getMessage());
			logger.info("-----尝试cmyk转化-----");
			File cmykJPEGFile = new File(srcUrl);
			try {
				BufferedImage image = ImageIO.read(cmykJPEGFile);
				ImageOutputStream output = ImageIO.createImageOutputStream(cmykJPEGFile);
				if (!ImageIO.write(image, "jpg", output)) {
					logger.info("-----cmyk转化异常:{}-----");
				}
				Thumbnails.of(image).size(width, height).watermark(pos, ImageIO.read(new File(targetUrl)), num)
						.outputQuality(num).toFile(targetUrl);
				logger.info("-----cmyk转化成功-----");
			} catch (IOException e1) {
				logger.info("-----cmyk转化异常:{}-----", e1.getMessage());
			}
		}
	}

	/**
	 * 裁剪
	 * 
	 * @param srcUrl
	 *            源图片地址
	 * @param targetUrl
	 *            目标图片地址
	 * @param width
	 *            宽
	 * @param height
	 *            高
	 * @param pos
	 *            显示位置: Positions.BOTTOM_RIGHT
	 * @param x
	 *            区域宽度
	 * @param y
	 *            区域高度
	 * @throws IOException
	 */
	public static void cut(String srcUrl, String targetUrl, int width, int height, Positions pos, int x, int y)
			throws IOException {
		try {
			Thumbnails.of(srcUrl).sourceRegion(pos, x, y).size(width, height).keepAspectRatio(false).toFile(targetUrl);
		} catch (IOException e) {
			logger.error("-----读取图片发生异常:{}-----", e.getMessage());
			logger.info("-----尝试cmyk转化-----");
			File cmykJPEGFile = new File(srcUrl);
			try {
				BufferedImage image = ImageIO.read(cmykJPEGFile);
				ImageOutputStream output = ImageIO.createImageOutputStream(cmykJPEGFile);
				if (!ImageIO.write(image, "jpg", output)) {
					logger.info("-----cmyk转化异常:{}-----");
				}
				Thumbnails.of(image).sourceRegion(pos, x, y).size(width, height).keepAspectRatio(false)
						.toFile(targetUrl);
				logger.info("-----cmyk转化成功-----");
			} catch (IOException e1) {
				logger.info("-----cmyk转化异常:{}-----", e1.getMessage());
			}
		}
	}

	/**
	 * 裁剪--指定坐标/大小
	 * 
	 * @param srcUrl
	 *            源图片地址
	 * @param targetUrl
	 *            目标图片地址
	 * @param width
	 *            宽
	 * @param height
	 *            高
	 * @param pointA_1
	 *            坐标A1
	 * @param pointA_2坐标A2
	 * @param pointB_1坐标B1
	 * @param pointB_2坐标B2
	 * @throws IOException
	 */
	public static void cut(String srcUrl, String targetUrl, int width, int height, int pointA_1, int pointA_2,
			int pointB_1, int pointB_2) throws IOException {
		try {
			Thumbnails.of(srcUrl).sourceRegion(pointA_1, pointA_2, pointB_1, pointB_2).size(width, height)
					.keepAspectRatio(false).toFile(targetUrl);
		} catch (IOException e) {
			logger.error("-----读取图片发生异常:{}-----", e.getMessage());
			logger.info("-----尝试cmyk转化-----");
			File cmykJPEGFile = new File(srcUrl);
			try {
				BufferedImage image = ImageIO.read(cmykJPEGFile);
				ImageOutputStream output = ImageIO.createImageOutputStream(cmykJPEGFile);
				if (!ImageIO.write(image, "jpg", output)) {
					logger.info("-----cmyk转化异常:{}-----");
				}
				Thumbnails.of(image).sourceRegion(pointA_1, pointA_2, pointB_1, pointB_2).size(width, height)
						.keepAspectRatio(false).toFile(targetUrl);
				logger.info("-----cmyk转化成功-----");
			} catch (IOException e1) {
				logger.info("-----cmyk转化异常:{}-----", e1.getMessage());
			}
		}
	}

	/**
	 * 转化图像格式
	 * 
	 * @param srcUrl
	 *            源图片地址
	 * @param targetUrl
	 *            目标图片地址
	 * @param width
	 *            宽
	 * @param height
	 *            高
	 * @param format
	 *            格式 如png/gif/jpg
	 * @throws IOException
	 */
	public static void format(String srcUrl, String targetUrl, int width, int height, String format)
			throws IOException {
		try {
			Thumbnails.of(srcUrl).size(width, height).outputFormat(format).toFile(targetUrl);
		} catch (IOException e) {
			logger.error("-----读取图片发生异常:{}-----", e.getMessage());
			logger.info("-----尝试cmyk转化-----");
			File cmykJPEGFile = new File(srcUrl);
			try {
				BufferedImage image = ImageIO.read(cmykJPEGFile);
				ImageOutputStream output = ImageIO.createImageOutputStream(cmykJPEGFile);
				if (!ImageIO.write(image, "jpg", output)) {
					logger.info("-----cmyk转化异常:{}-----");
				}
				Thumbnails.of(image).size(width, height).outputFormat(format).toFile(targetUrl);
				logger.info("-----cmyk转化成功-----");
			} catch (IOException e1) {
				logger.info("-----cmyk转化异常:{}-----", e1.getMessage());
			}
		}

	}
}
